/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Assignment.DAO;

import Assignment.DTO.UserDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dieptn
 */
public class UserDAO {

    public static ArrayList<UserDTO> search(Connection con, UserDTO user) {
        ArrayList<UserDTO> result = new ArrayList<UserDTO>();
        String sql = "";
        PreparedStatement stm = null;
        try {
            if (user == null) {
                sql = "SELECT * FROM Users";
                stm = con.prepareStatement(sql);
            }
            //stm.setString(1, user.getUsername());
            //stm.setString(2, user.getPassword());
            ResultSet rs = stm.executeQuery();
            UserDTO userTemp = null;
            while (rs.next()) {
                userTemp = new UserDTO();
                userTemp.setUsername(rs.getString(1));
                userTemp.setPassword(rs.getString(2));
                userTemp.setEmail(rs.getString(3));
                userTemp.setRealName(rs.getString(4));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                userTemp.setDateOfBirth(sdf.parse(rs.getDate(5).toString()));
                userTemp.setAddress(rs.getString(6));
                userTemp.setCity(rs.getString(7));
                userTemp.setPhone(rs.getString(8));
                userTemp.setIsAdmin(rs.getBoolean(9));
                result.add(userTemp);
            }
            stm.close();
            con.close();
        } catch (Exception ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public static int insert(Connection con, UserDTO user) {
        int result = 0;
        try {
            String sql = "INSERT INTO Users VALUES(?,?,?,?,?,?,?,?,?)";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, user.getUsername());
            stm.setString(2, user.getPassword());
            stm.setString(3, user.getEmail());
            stm.setString(4, user.getRealName());
            stm.setTimestamp(5, new java.sql.Timestamp(user.getDateOfBirth().getTime()));
            stm.setString(6, user.getAddress());
            stm.setString(7, user.getCity());
            stm.setString(8, user.getPhone());
            stm.setBoolean(9, user.getIsAdmin());
            result = stm.executeUpdate();
            stm.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int update(Connection con, UserDTO user) {
        int result = 0;
        try {
            String query = "UPDATE Users SET password=?, email=?, realname=?, "
                    + "dateofbirth=?, address=?, city=?, phone=?, isAdmin=? "
                    + "WHERE username=?";
            PreparedStatement stm = con.prepareStatement(query);
            stm.setString(1, user.getPassword());
            stm.setString(2, user.getEmail());
            stm.setString(3, user.getRealName());
            stm.setTimestamp(4, new java.sql.Timestamp(user.getDateOfBirth().getTime()));
            stm.setString(5, user.getAddress());
            stm.setString(6, user.getCity());
            stm.setString(7, user.getPhone());
            stm.setBoolean(8, user.getIsAdmin());
            stm.setString(9, user.getUsername());
            result = stm.executeUpdate();
            stm.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int delete(Connection con, String username) {
        int result = 0;
        try {
            String sql = "DELETE FROM Users WHERE username=?";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, username);
            result = stm.executeUpdate();
            stm.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
